package bulk_param.numspe.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;

import bulk_param.dao.OuvertureNumSpeDao;
import bulk_param.dao.VerificationMaxPointIDDao;
import bulk_param.modele.NumSpePointModel;
import bulk_param.view.NumSpe_Open_View;


public class ButtonNumSpeOuvertureController implements ActionListener{
	
	private NumSpe_Open_View numSpe_Open_View;
	private OuvertureNumSpeDao ouvertureNumSpeDao;
	private NumSpePointModel numSpePointModel;
	private VerificationMaxPointIDDao verificationMaxPointIDDao;
	private String indicatif="33";

	
	
	public ButtonNumSpeOuvertureController(NumSpe_Open_View numSpe_Open_View){
		this.numSpe_Open_View=numSpe_Open_View;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		//Remise des contours champs à gris	
		numSpe_Open_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		numSpe_Open_View.txtZLT.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		numSpe_Open_View.widerDropDownCombo.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		numSpe_Open_View.dateChooser.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		
		numSpePointModel = new NumSpePointModel();
		verificationMaxPointIDDao = new VerificationMaxPointIDDao();
		if(verificationMaxPointIDDao.isMaxPointIDEgaux()){
		
		if(numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString().trim().isEmpty()){
			numSpe_Open_View.widerDropDownCombo.setBorder(BorderFactory.createLineBorder(Color.red));
			}
		
		if(numSpe_Open_View.dateChooser.getDate()==null){
			numSpe_Open_View.dateChooser.setBorder(BorderFactory.createLineBorder(Color.red));
		}
		
		if(numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString().trim().isEmpty() ||
				numSpe_Open_View.txtZABPQ.getText().isEmpty()||	numSpe_Open_View.dateChooser.getDate()==null){
			JOptionPane.showMessageDialog(null,"Tout les champs sont obligatoires","Attention",JOptionPane.WARNING_MESSAGE);				
		}else if(numSpe_Open_View.txtZABPQ.getText().isEmpty()){
			numSpe_Open_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.red));
		}else{
		if(!numSpe_Open_View.txtZABPQ.getText().isEmpty() && !numSpe_Open_View.txtZABPQ.getText().matches(numSpePointModel.getZABPQ_REGEX())){
			numSpe_Open_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.red));
			JOptionPane.showMessageDialog(null,numSpePointModel.getZABPQ_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);							
		}else{	
		if((numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString().substring(3, 4).equalsIgnoreCase("c") && numSpe_Open_View.txtZABPQ.getText().length()>5) || 
			(numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString().substring(3, 4).equalsIgnoreCase("c") && numSpe_Open_View.txtZABPQ.getText().substring(1, 2).equalsIgnoreCase("8")) ||
			(numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString().substring(3, 4).equalsIgnoreCase("8") && !numSpe_Open_View.txtZABPQ.getText().substring(1, 2).equalsIgnoreCase("8"))||
			(numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString().substring(0, 1).equalsIgnoreCase("R") && !numSpe_Open_View.txtZABPQ.getText().substring(1, 2).equalsIgnoreCase("1"))){
			System.out.println("INCOHERENCE DE DONNEES");
			if(numSpe_Open_View.txtZABPQ.getText().substring(1, 2).equalsIgnoreCase("3")||(numSpe_Open_View.txtZABPQ.getText().substring(1, 2).equalsIgnoreCase("1") && numSpe_Open_View.txtZABPQ.getText().length()<=5)){
				System.out.println("NUM COURT");
				JOptionPane.showMessageDialog(null,"Le tarif choisi ne peut-être \nattribué a un numéro court 3BPQ","Attention",JOptionPane.WARNING_MESSAGE);					
			}
			if(numSpe_Open_View.txtZABPQ.getText().substring(1, 2).equalsIgnoreCase("8")){
				System.out.println("NUMERO 800");
				JOptionPane.showMessageDialog(null,"Le tarif choisi ne peut-être \nattribué a un numéro 800","Attention",JOptionPane.WARNING_MESSAGE);					
			}
			if(numSpe_Open_View.txtZABPQ.getText().substring(1, 2).equalsIgnoreCase("1") && numSpe_Open_View.txtZABPQ.getText().length()>5){
				System.out.println("RENSEIGNEMENT");
				JOptionPane.showMessageDialog(null,"Le tarif choisi ne peut-être \nattribué a un numéro de renseignement\n118-XYZ","Attention",JOptionPane.WARNING_MESSAGE);					
			}
		}else{
		//ENREGRISTREMENT SI TOUT EST OK
		if(!numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString().trim().isEmpty() &&
				!numSpe_Open_View.txtZABPQ.getText().isEmpty()&& numSpe_Open_View.dateChooser.getDate()!=null){
			//Verification si le point existe deja
			ouvertureNumSpeDao = new OuvertureNumSpeDao();
			boolean isExist = ouvertureNumSpeDao.isPointExist("33",numSpe_Open_View.txtZABPQ.getText().substring(1));
			
			if(isExist == true){
				String msg = "Le point : 33" + numSpe_Open_View.txtZABPQ.getText().substring(1) + "\néxiste déjà";
                JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);							
			}
			else{
				//Enregistrement des valeurs
				Timestamp dateInBase =null;
				try {
					dateInBase = ouvertureNumSpeDao.dateInBase(indicatif, numSpe_Open_View.txtZABPQ.getText().substring(1));
					System.out.println("MA DATE EN BASE : "+dateInBase);
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (dateInBase==null){
					int entrepriseCPTL = 1;
					int entrepriseDARTY = 2;
					String access_region = ouvertureNumSpeDao.numberJurisdiction(numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString());
					String pointZABPQ = indicatif+numSpe_Open_View.txtZABPQ.getText().substring(1);
					Date date = numSpe_Open_View.dateChooser.getDate();
					String zLT = numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString();
					int isPrefix=0;
					if(numSpe_Open_View.txtZABPQ.getText().matches(numSpePointModel.getNUM_0800_REGEX())){
						isPrefix=1;
					}else{
						isPrefix=0;
					}
					System.out.println("MES VALEURS DENTRES : ");
					int pointClass = Integer.valueOf(access_region.trim());
					System.out.println("access_region correspondant au tarif : "+access_region);
					System.out.println("ZABPQ : "+pointZABPQ);
					System.out.println("date : "+date);
					System.out.println("ZLT : "+zLT);
					System.out.println("isAprefix : "+isPrefix);
					String success1=ouvertureNumSpeDao.openPointNumSpe(pointClass, pointZABPQ,isPrefix, date, access_region,zLT, entrepriseCPTL,"C");					
					String success2=ouvertureNumSpeDao.openPointNumSpe(pointClass, pointZABPQ,isPrefix, date,access_region, zLT, entrepriseDARTY,"C");						
					if(success1=="ko" || success2=="ko" ){
						if(success1=="ko"){
							String msg= "Le point : " +indicatif+ numSpe_Open_View.txtZABPQ.getText().substring(1) + "\nen 'point_category'=1 n'a pas été créé";
							JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);										
						}
						if(success2=="ko"){
							String msg= "Le point : " +indicatif+ numSpe_Open_View.txtZABPQ.getText().substring(1) + "\nen 'point_category'=2 n'a pas été créé";
							JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);							
						}									
					}
					if(success1=="ok" && success2=="ok" ){
						String msg = "Le point : 33" + numSpe_Open_View.txtZABPQ.getText().substring(1) + "\na été créé avec succès";
		                JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);			
					}	
				}else{
				try{
					if(ouvertureNumSpeDao.isDateBefore(indicatif,numSpe_Open_View.txtZABPQ.getText().substring(1),numSpe_Open_View.dateChooser.getDate())==false){
						int entrepriseCPTL = 1;
						int entrepriseDARTY = 2;
						String access_region = ouvertureNumSpeDao.numberJurisdiction(numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString());
						String pointZABPQ = indicatif+numSpe_Open_View.txtZABPQ.getText().substring(1);
						Date date = numSpe_Open_View.dateChooser.getDate();
						String zLT = numSpe_Open_View.widerDropDownCombo.getSelectedItem().toString();
						int pointClass = Integer.valueOf(access_region.trim());
						int isPrefix=0;
						if(numSpe_Open_View.txtZABPQ.getText().matches(numSpePointModel.getNUM_0800_REGEX())){
							isPrefix=1;
						}else{
							isPrefix=0;
						}
						System.out.println("MES VALEURS DENTRES : ");
						System.out.println("access_region correspondant au tarif : "+access_region);
						System.out.println("ZABPQ : "+pointZABPQ);
						System.out.println("date : "+date);
						System.out.println("ZLT : "+zLT);
						String success1=ouvertureNumSpeDao.openPointNumSpeplus1sec(pointClass, pointZABPQ,isPrefix, date, access_region,zLT, entrepriseCPTL,dateInBase,"C");					
						String success2=ouvertureNumSpeDao.openPointNumSpeplus1sec(pointClass, pointZABPQ,isPrefix, date,access_region, zLT, entrepriseDARTY,dateInBase,"C");					
						if(success1=="ko" || success2=="ko" ){
							if(success1=="ko"){
								String msg= "Le point : " +indicatif+ numSpe_Open_View.txtZABPQ.getText().substring(1) + "\nen 'point_category'=1 n'a pas été créé";
								JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);										
							}
							if(success2=="ko"){
								String msg= "Le point : " +indicatif+ numSpe_Open_View.txtZABPQ.getText().substring(1) + "\nen 'point_category'=2 n'a pas été créé";
								JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);							
							}									
						}
						if(success1=="ok" && success2=="ok" ){
							String succesMsg= "Le point : " +indicatif+ numSpe_Open_View.txtZABPQ.getText().substring(1) + "\na été recréé avec succès";
							JOptionPane.showMessageDialog(null, succesMsg, "Message", JOptionPane.INFORMATION_MESSAGE);	
							}	
						}else{
						String dateMsg="Vous devez choisir une date \npostérieure ou égale à la date :"+new java.sql.Date(dateInBase.getTime());
						JOptionPane.showMessageDialog(null, dateMsg, "Attention", JOptionPane.WARNING_MESSAGE);	
						}					
						} catch (ParseException e) {
					// TODO Auto-generated catch block
							e.printStackTrace();
				}
					}
			}
		
			ouvertureNumSpeDao.closeConnection();
						}
					}
				}
			}
		}else{
			String message="L'opération a été annulée.\nUn problème lié au max point_id en est la cause";
			JOptionPane.showMessageDialog(null, message, "Attention", JOptionPane.WARNING_MESSAGE);	
		}
	}						
}	


